﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HelloOne.NewsSvc.Models;
using HelloOne.NewsSvc.ViewModels;
using HelloOne.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HelloOne.NewsSvc.Controllers
{
    /// <summary>
    /// 资讯
    /// </summary>
    [Produces("application/json")]
    [Route("api/News")]
    public class NewsController : Controller
    {
        private IRepository<News> _repository;
        public NewsController(IRepositoryProvider provider)
        {
            _repository = provider.Create<News>();
        }

        // GET: api/News
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/News/5
        [HttpGet("{id}")]
        public News Get(Guid id)
        {
            return _repository.FindSingle(x => x.ID == id);
        }

        // POST: api/News
        [HttpPost]
        public void Post([FromBody]NewsVM vm)
        {
            var news = new News(vm.Title,"ltp", vm.Content,3);
            _repository.Save(news);

            var ax = _repository.FindSingle(x => x.ID == news.ID);
            if (ax.IsDeleted)
            {
                ax.IsDeleted = false;
                _repository.Save(ax);
            }
        }

        // PUT: api/News/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
